//
//  DataListViewController.m
//  Bus_iPhone
//
//  Created by 修勇 黄 on 12-7-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DataListViewController.h"
#import "BusDataManager.h"
#import "BusSetting.h"

@interface DataListViewController ()

@end

@implementation DataListViewController

@synthesize list=_list;
@synthesize selectIndexPath=_selectIndexPath;


-(void)dealloc{

    [_selectIndexPath release];
    [_list release];
    [super dealloc];
}


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.list=[[BusSetting defaultSetting]getDownLoadDataList];
    [self setCustomNavigationReturnItem];
   
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [_list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (!cell) {
        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    NSString *fileName=[_list objectAtIndex:indexPath.row];
    NSString *cityName=[fileName substringWithRange:NSMakeRange(0, fileName.length-3)];
    cell.textLabel.text=cityName;
    if ([cityName isEqualToString:[[BusDataManager currentBusDataManager]cityName]]) {
        cell.accessoryType=UITableViewCellAccessoryCheckmark;
        self.selectIndexPath=indexPath;
    }else {
        cell.accessoryType=UITableViewCellAccessoryNone;
    }
    return cell;
}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType=UITableViewCellAccessoryCheckmark) {
        return NO;
    }else
        return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *fileName=[_list objectAtIndex:indexPath.row];
    if ([[BusSetting defaultSetting]removeDataByFileName:fileName]) {
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType==UITableViewCellAccessoryNone) {
        UITableViewCell *lastSelectCell=[tableView cellForRowAtIndexPath:_selectIndexPath];
        lastSelectCell.accessoryType=UITableViewCellAccessoryNone;
        self.selectIndexPath=indexPath;
        cell.accessoryType=UITableViewCellAccessoryCheckmark;
        NSString *fileName=[_list objectAtIndex:indexPath.row];
        [[BusSetting defaultSetting]setBusDataFilePathByFileName:fileName];
    }
}

@end
